package com.zzsy.shoppingpro.home.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.app.Config;
import com.zzsy.shoppingpro.home.bean.HomeBean;

import java.util.List;

/**
 * Created by Administrator on 2019/8/20 0020.
 */

public class RecommendGridViewAdapter extends BaseAdapter {
    private Context context;
    private List<HomeBean.ResultBean.RecommendInfoBean> data;

    public RecommendGridViewAdapter(Context mContext, List<HomeBean.ResultBean.RecommendInfoBean> recommend_info) {
        this.context = mContext;
        this.data = recommend_info;
    }

    @Override
    public int getCount() {
        if (data.size() >= 6) {
            return 6;
        } else {
            return data.size();
        }

    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_recommend_grid_view, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        HomeBean.ResultBean.RecommendInfoBean recommendInfoBean = data.get(position);
        viewHolder.tvName.setText(recommendInfoBean.getName());
        viewHolder.tvPrice.setText(recommendInfoBean.getCover_price());
        Glide.with(context)
                .load(Config.BASE_URl_IMAGE + recommendInfoBean.getFigure())
                .into(viewHolder.ivRecommend);
        return convertView;
    }

    class ViewHolder {
        ImageView ivRecommend;
        TextView tvName;
        TextView tvPrice;

        public ViewHolder(View convertView) {
            ivRecommend = convertView.findViewById(R.id.iv_recommend);
            tvName = convertView.findViewById(R.id.tv_name);
            tvPrice = convertView.findViewById(R.id.tv_price);
        }
    }
}
